package org.example.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.Data;
import org.example.seckill.bean.User;
import org.example.seckill.bean.goods;
import org.example.seckill.bean.seckillGoods;
import org.example.seckill.mapper.goodsMapper;
import org.example.seckill.mapper.seckillGoodsMapper;
import org.example.seckill.service.seckillGoodsService;
import org.example.seckill.util.timeUtil;
import org.example.seckill.vo.SeckillStatus;
import org.example.seckill.vo.goodsVo;
import org.example.seckill.vo.orderStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author woopsion
 * @date 2025-06-30
 */
@Service
public class seckillGoodsServiceImpl extends
        ServiceImpl<seckillGoodsMapper, seckillGoods> implements seckillGoodsService{
    @Autowired
    seckillGoodsMapper seckillGoodsMapper;

    @Autowired
    goodsMapper goodsMapper;
    
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 
     * @param goodsId
     * @return integer 1  表示商品未开始
     * 2 表示进行中
     * 3 表示已结束
     * -1 表示商品不存在
     */
    @Override
    public SeckillStatus getSeckillStatusByGoodsId(Long goodsId) {
        //判断商品是否存在
        seckillGoods goods = (seckillGoods)redisTemplate.opsForValue().get("goods:detail:seckill:" + goodsId);
        if(goods == null){
            goods= seckillGoodsMapper.selectById(goodsId);
            if(goods==null){return SeckillStatus.SALE_GOODS_UNEXIST;}
            redisTemplate.opsForValue().set("goods:detail:seckill:" + goodsId,goods,60, TimeUnit.SECONDS);
        }    
        
        Date startDate = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date now = new Date();
        if( now.before(startDate)) return SeckillStatus.SALE_READY;
        if (now.before(endDate)) return SeckillStatus.SALE_ING;
        return SeckillStatus.SALE_SUCCESS;
    }


}
